import { defineStore } from "pinia";

const useHomeStore=defineStore('home',{
	state:()=>({
		search_input:'',
		empty:0,
		latitude:uni.getStorageSync('latitude'),
		longitude:uni.getStorageSync('longitude'),
		search_list:[],
		rules:'',
		gole_address:'',
		gole_name:'',
		gole_city:'',
		points_list:[],
		polyline_list:[],
		markers:[],
	}),
	actions:{
		startLocation(){
			 uni.getLocation({
				type:'gcj02',
				isHighAccuracy:true,
				success:(res)=>{
					uni.setStorageSync('latitude',res.latitude)
					uni.setStorageSync('longitude',res.longitude)
					uni.setStorageSync('selfLatitude',res.latitude)
					uni.setStorageSync('selfLongitude',res.longitude)
				},
				fail: (error) => {
					uni.$u.toast(`定位失败`)
				},
			})
		},
		 refleshSelfLocation(){
			 uni.getLocation({
				type:'gcj02',
				isHighAccuracy:true,
				success:(res)=>{
					uni.setStorageSync('selfLatitude',res.latitude)
					uni.setStorageSync('selfLongitude',res.longitude)
					uni.$u.toast(`刷新成功`)
				},
				fail: (error) => {
					uni.$u.toast(`刷新失败`)
				}
			})
		},
		async searchlocation(hospitalStore){
			if (this.search_input==''){
				uni.$u.toast(`搜索内容不能为空`)
			}else if(hospitalStore.showStart===2){
				uni.$u.toast(`必须结束当前定位才能查询`)
			}else{
				const res=await uni.request({
					url:'https://restapi.amap.com/v3/assistant/inputtips',
					method:'get',
					data:{
						key:'4eac491217889d34536b62dacfa8d6f6',
						keywords:this.search_input
					}
				})
				const data=res.data
				if(data.status==='1'){
					this.search_list=data.tips
					this.empty=1
					this.search_input=''
				}else{
					uni.$u.toast(`搜索失败`)
				}
			}
		},
		chooseItem(gole_location,gole_address,gole_name,gole_city,store,self,marker,polyline,marker_dict,gole_location_marker){
			this.gole_location=gole_location
			this.gole_address=gole_address
			this.gole_name=gole_name
			this.gole_city=gole_city
			store.total(self,marker,polyline,marker_dict,gole_location_marker)
		},
		map_self(){
			this.latitude=uni.getStorageSync('latitude')
			this.longitude=uni.getStorageSync('longitude')
		},
		map_hospital(){
			this.latitude=uni.getStorageSync('hospital_latitude')
			this.longitude=uni.getStorageSync('hospital_longitude')
		},
		map_gole(){
			const conirm=uni.getStorageSync('gole_latitude')
			if(!conirm){
				uni.$u.toast(`你还没有输入目的地`)
			}else{
				this.latitude=uni.getStorageSync('gole_latitude')
				this.longitude=uni.getStorageSync('gole_longitude')
			}
		},
		map_reflesh(hospitlStore,goleStore,store,marker_dict2){
			if(hospitlStore.showStart===2){
				hospitlStore.refleshHospital(store,marker_dict2)
			}else if(goleStore.loading===2){
				goleStore.refleshGole(store,marker_dict2)
			}else{
				uni.$u.toast('还没有开始定位')
			}
		}
	}
})
export default useHomeStore;